//
//  Brands_BrandsDetail_Tbv_WX.m
//  ZITHER
//
//  Created by weixin on 2019/5/22.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Brands_BrandsDetail_Tbv_WX.h"


#import "PicAndTextTableView.h"
#import "EvaluateTableView.h"
//#import "YBTitleV.h"
#import "Brands_BrandsDetailHead_Cell_WX.h"
#import "Brands_BrandsDetailItem_Cell_WX.h"
#import "Main_Title_View_WX.h"


@interface Brands_BrandsDetail_Tbv_WX ()<UITableViewDelegate, UITableViewDataSource>

//
@property (nonatomic, strong) UIScrollView *subScrollView;

//@property (nonatomic, strong) YBTitleV *TitleV;
@property (nonatomic, strong) EvaluateTableView *evaluateTableView;
@property (nonatomic, strong) PicAndTextTableView *picAndTextTableView;
@property (nonatomic, strong) Main_Title_View_WX *TitleV;

@end


@implementation Brands_BrandsDetail_Tbv_WX


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.subScrollView]) {
        self.evaluateTableView.scrollEnabled = NO;
        self.picAndTextTableView.scrollEnabled = NO;
    }
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if ([scrollView isEqual:self.subScrollView]) {
        self.evaluateTableView.scrollEnabled = YES;
        self.picAndTextTableView.scrollEnabled = YES;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if ([scrollView isEqual:self]) {
        
        NSLog(@"%lf, %lf", scrollView.contentOffset.y, scrollView.contentSize.height-scrollView.H);
        
        if (scrollView.contentOffset.y >= (scrollView.contentSize.height-scrollView.H-0.5)) {
            self.offsetType = OffsetTypeMax;
        } else if (scrollView.contentOffset.y <= 0) {
            self.offsetType = OffsetTypeMin;
        } else {
            self.offsetType = OffsetTypeCenter;
        }
        
        if (self.TitleV.index == 0 && self.picAndTextTableView.offsetType == OffsetTypeCenter) {
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentSize.height-scrollView.H);
        }
        
        if (self.TitleV.index == 1 && self.evaluateTableView.offsetType == OffsetTypeCenter) {
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentSize.height-scrollView.H);
        }
        
    } else if ([scrollView isEqual:self.subScrollView]) {
        if (self.subScrollView.contentOffset.x/kScreenW == 0) {
            [self.TitleV changeBtn:0];
        }else if (self.subScrollView.contentOffset.x/kScreenW == 1){
            [self.TitleV changeBtn:1];
        }
//        [self.TitleV configAnimationOffsetX:self.subScrollView.contentOffset.x];
    }
    
    
}


#pragma mark *** UITableViewDataSource ***
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section == 0) {
//        return 400;
//    } else {
//        return kHeightOfGoodsDetailsBottomCell;
//    }
//}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 44;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        return [self getHeadCell:indexPath];
        
    }
    else {
        
        return [self getBrands_BrandsDetailItem_Cell_WX:indexPath];
    }
}


-(UITableViewCell *)getHeadCell:(NSIndexPath *)indexPath {
    Brands_BrandsDetailHead_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:@"Brands_BrandsDetailHead_Cell_WX"];
    
    if (!cell) {
        cell = [[Brands_BrandsDetailHead_Cell_WX alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Brands_BrandsDetailHead_Cell_WX"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



-(UITableViewCell *)getBrands_BrandsDetailItem_Cell_WX:(NSIndexPath *)indexPath {
    //    goodVOsModel * model =  self.arrM[indexPath.row];

    Brands_BrandsDetailItem_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:@"Brands_BrandsDetailItem_Cell_WX"];
    if (!cell) {
        cell = [[Brands_BrandsDetailItem_Cell_WX alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Brands_BrandsDetailItem_Cell_WX"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell.type) {
        cell.type = YES;
        [cell addSubview:self.subScrollView];
    }
    
    return cell;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return self.TitleV;
    }
    return nil;
}


#pragma mark *** getter ***

-(Main_Title_View_WX *)TitleV{
    if (!_TitleV) {
        _TitleV = [[Main_Title_View_WX alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 40 )];
        [_TitleV setShadowType1WithColor:[UIColor defaultShadowColor] offset:CGSizeMake(0, 5) opacity:1 radius:3];
        
        NSMutableArray *arrMNormal = [[NSMutableArray alloc]initWithArray:@[@"碗表",@"详情"]];
        _TitleV.arrMNormal = arrMNormal;
        _TitleV.state = MainTitleVStateleft;
        _TitleV.BtnFind.hidden = YES;
        _TitleV.BtnNew.hidden = YES;
        
        
        @weakify(self);
        [_TitleV setBlcokIndex:^(NSInteger index) {
            @strongify(self);
            self.subScrollView.contentOffset = CGPointMake(index*kScreenW,0);
        }];
    }
    return _TitleV;
}


-(UIScrollView *)subScrollView{
    if (!_subScrollView) {
        _subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHeightOfGoodsDetailsBottomCell)];
        _subScrollView.bounces = NO;
        [_subScrollView setContentSize:CGSizeMake(SCREEN_WIDTH*2, kHeightOfGoodsDetailsBottomCell)];
        _subScrollView.pagingEnabled = YES;
        _subScrollView.showsHorizontalScrollIndicator = NO;
        _subScrollView.delegate = self;
        [_subScrollView addSubview:self.picAndTextTableView];
        [_subScrollView addSubview:self.evaluateTableView];

    }
    return _subScrollView;
}

-(PicAndTextTableView *)picAndTextTableView{
    if (!_picAndTextTableView) {
        _picAndTextTableView = [[PicAndTextTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kHeightOfGoodsDetailsBottomCell) style:UITableViewStylePlain];
        _picAndTextTableView.backgroundColor = [UIColor whiteColor];
        _picAndTextTableView.mainVC = (Brands_BrandsDetail_VC_WX*)[self getViewController:[Brands_BrandsDetail_VC_WX class]];
    }
    return _picAndTextTableView;
}

-(EvaluateTableView *)evaluateTableView{
    if (!_evaluateTableView) {
        _evaluateTableView = [[EvaluateTableView alloc] initWithFrame:CGRectMake(kScreenW, 0, kScreenW, kHeightOfGoodsDetailsBottomCell) style:UITableViewStylePlain];
        _evaluateTableView.backgroundColor = [UIColor whiteColor];
        _evaluateTableView.mainVC = (Brands_BrandsDetail_VC_WX*)[self getViewController:[Brands_BrandsDetail_VC_WX class]];
    }
    return _evaluateTableView;
}





@end
